package com.zhuoyue.web.controller;

import javax.servlet.http.HttpServletRequest;


import com.zhuoyue.biz.core.api.dto.rep.AppUserHomeRelationRepDTO;
import com.zhuoyue.biz.core.api.service.impl.AppUserHomeRelationService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import com.zhuoyue.common.dto.BaseResultDTO;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zhuoyue.biz.core.api.domain.AppUserHomeRelationViewDO;
import com.zhuoyue.common.dto.ControllerResult;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wjming
 * @ClassName: AppUserHomeRelationController
 * @Description: 房间家庭关系控制接口
 * @Date 2021/4/17 12:42
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/relationSpeaker")
@Validated
public class AppUserHomeRelationController {
    @Autowired
    private AppUserHomeRelationService appUserHomeRelationService;

    /**
     * @Description: 获取语音设备与家庭关系 此接口是根据token解析获取用户的ID，通过用户ID获取所有语音设备与家庭的关系信息
     * @param: request
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021/4/17 12:43
     */
    @GetMapping("getRelationSpeaker")
    @CheckLogin
    public ControllerResult getRelationSpeakerByAppUserId(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        List<AppUserHomeRelationViewDO> result = appUserHomeRelationService
                .getRelationSpeakByAppUserId(user.getId());
        List<AppUserHomeRelationRepDTO> appUserHomeRelationRepDtoS = new ArrayList<>();
        if (result.size() > 0) {
            result.forEach(iList -> {
                AppUserHomeRelationRepDTO appUserHomeRelationRepDTO = new AppUserHomeRelationRepDTO();
                BeanUtils.copyProperties(iList, appUserHomeRelationRepDTO);
                appUserHomeRelationRepDtoS.add(appUserHomeRelationRepDTO);

            });
        }
        return ControllerResult.toReturnDataSuccess("获取成功", appUserHomeRelationRepDtoS);
    }

    /**
     * 修改语音设备可用设备
     * 除了若琪和小可之外还有小度和天猫精灵
     *
     * @param voiceType   语音类型 0:若琪, 1:为小可, 2:为小度, 3:为天猫精灵
     * @param selectStr   给语音的
     * @param noSelectStr 不给语音的
     * @return
     * @deprecated 还未检查
     */
    @PutMapping("updateRokidAndAISpeak")
    @CheckLogin
    public ControllerResult updateVoice(@NotNull("语音类型不能为空") Integer voiceType,
                                        @NotNull @RequestParam(value = "selectList", required = false) String selectStr,
                                        @NotNull @RequestParam(value = "noSelectList", required = false) String noSelectStr) {
        BaseResultDTO result = appUserHomeRelationService.updateSpeechCipheringEquipment(voiceType, selectStr, noSelectStr);
        if (result.isFailed()) {
            return ControllerResult.toOtherFail(result.getErrorDetail(), result.getResultCode());
        }
        return ControllerResult.toReturnMessageSuccess("修改成功");
    }

}
